﻿/**
 * Copyright (c) 2010, Microsoft Corporation
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *       Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *       
 *       Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *       
 *       Neither the name of Microsoft nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Microsoft ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Microsoft BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebRole
{
    public partial class _Default : System.Web.UI.Page
    {
        const string ENV_X_DRIVES = "X_DRIVES";

        /// <summary>
        /// Page Load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            System.Diagnostics.Trace.TraceInformation("Loading Default.aspx");
            lblHeading.Text = "Hello WebRole";

            lblXDrivesEnvVar.Text = GetXDriveEnv(GetEnv());
        }

        /// <summary>
        /// Get listing of all Environment Variables
        /// </summary>
        /// <returns>Dictionary of Environment Variables</returns>
        private IDictionary<string, string> GetEnv()
        {
            //Get all the environment variables into a collection
            System.Collections.ICollection id = System.Environment.GetEnvironmentVariables();

            //Get the Enumerator from the collection
            System.Collections.IEnumerator ie = id.GetEnumerator();

            //Create the working defaults. The object returned by the Enumerator is a 
            //DictionaryEntry so we create a default one
            int i = 0;
            System.Collections.DictionaryEntry de;
            IDictionary<string, string> dicEnv = new Dictionary<string, string>();

            //Loop through the collection
            for (i = 0; i < id.Count; i++)
            {
                //string s = ie.Current.ToString();
                ie.MoveNext();

                //Get the dictionary entry and update the contents of the array
                de = (System.Collections.DictionaryEntry)ie.Current;

                dicEnv.Add(de.Key.ToString(), de.Value.ToString());
            }

            return dicEnv;
        }

        /// <summary>
        /// Get X_DRIVES environment variable
        /// </summary>
        /// <param name="dicEnv">Dictionary of Environment Variables</param>
        /// <returns>X_DRIVES value</returns>
        private string GetXDriveEnv(IDictionary<string, string> dicEnv)
        {
            foreach (KeyValuePair<string, string> kvp in dicEnv)
            {
                if (kvp.Key.StartsWith(ENV_X_DRIVES))
                {
                    return kvp.Value;
                }
            }

            return String.Empty;
        }
    }
}
